//
//  StatusFrame.m
//  微博2期
//
//  Created by J on 15/11/29.
//
//

#import "StatusFrame.h"
#import "Status.h"
#import "User.h"
#define StatusCellBorderW 10 //cell边框宽度


@implementation StatusFrame

- (CGSize)sizeWithText:(NSString*)text font:(UIFont*)font maxW:(CGFloat)maxW{
    
    NSMutableDictionary * atts = [NSMutableDictionary dictionary];
    atts[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:atts context:nil].size;
}



- (void)setStatus:(Status *)status{
    _status = status;

    //cell宽度
    CGFloat cellW = [UIScreen mainScreen].bounds.size.width;
    /**
     *  原创微博整体
     */
    
    
    /**
     *  头像
     */
    CGFloat iconW = 35;
    CGFloat iconX = StatusCellBorderW;
    CGFloat iconY = StatusCellBorderW;
    self.iconViewF = CGRectMake(iconX, iconY, iconW, iconW);
    
    /**
     *  昵称
     */
    
    CGFloat nameX = CGRectGetMaxX(self.iconViewF)+ StatusCellBorderW;
    CGFloat nameY = iconY;
    CGSize nameSize = [self sizeWithText:status.user.name font:StatusCellNameFont maxW:MAXFLOAT];
//    self.nameLabelF = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
    self.nameLabelF = (CGRect){{nameX,nameY},nameSize};

    /**
     *  会员图标
     */
    
    if ( status.user.isVip){
        CGFloat vipX  = CGRectGetMaxX(self.nameLabelF) + StatusCellBorderW;
        CGFloat vipY = nameY;
        CGFloat vipH =nameSize.height;
        CGFloat vipW = 14;
        self.vipViewF = CGRectMake(vipX, vipY, vipW, vipH);
    }
    
    
    /**
     *  时间
     */
    
     CGFloat timeX = nameX;
    CGFloat timeY = CGRectGetMaxY(self.nameLabelF)+StatusCellBorderW;
    CGSize timeSize = [self sizeWithText:status.created_at font:StatusCellTimeFont maxW:MAXFLOAT];
    self.timeLabelF = (CGRect){{timeX,timeY},timeSize};
    
    /**
     *  来源
     */
    
    
    CGFloat sourceX = CGRectGetMaxX(_timeLabelF)+StatusCellBorderW;
    CGFloat sourceY = timeY;
    CGSize sourceSize = [self sizeWithText:status.created_at font:StatusCellTimeFont maxW:MAXFLOAT];
    self.sourceLabelF = (CGRect){{sourceX,sourceY},sourceSize};

    
    /**
     *  正文
     */

    CGFloat contentX = iconX;
    CGFloat contentY = MAX(CGRectGetMaxY(self.iconViewF), CGRectGetMaxY(self.timeLabelF))+StatusCellBorderW;
    CGFloat maxW = cellW - 2*contentX;
    CGSize contentSize = [self sizeWithText:status.text font:StatusContentFont maxW:maxW];
    self.contentLabelF = (CGRect){{contentX,contentY},contentSize};
    

    
    
    /**
     *  cell高度
     */

    
    
    /**
     *  配图
     */
    CGFloat orginalH = 0;
    if (status.pic_urls.count) {//有配图
        CGFloat photoWH = 100;
        CGFloat photoX = contentX;
        CGFloat photoY = CGRectGetMaxY(self.contentLabelF) + StatusCellBorderW;
        self.photoViewF = CGRectMake(photoX, photoY, photoWH, photoWH);
        orginalH = CGRectGetMaxY(self.photoViewF) + StatusCellBorderW;
    }else{//无配图
        orginalH = CGRectGetMaxY(self.contentLabelF) + StatusCellBorderW;
        self.photoViewF = CGRectZero;
    }
   
    
    
    
    /**
     *  原创微博整体
     */
    
    
    
    CGFloat originalX = 0;
    CGFloat originalY = 15;
    CGFloat originalW  = cellW;
    CGFloat originalH = CGRectGetMaxY(self.contentLabelF) + StatusCellBorderW;

    if (status.pic_urls.count) {//如果原创微博有配图 那么高度从配图底部开始计算
         originalH = CGRectGetMaxY(self.photoViewF) + StatusCellBorderW;
    }
    self.originalViewF = CGRectMake(originalX, originalY, originalW, originalH);
    
    CGFloat toolBarY = 0;

    /** 被转发微博*/
    /** */
    if (status.retweeted_status) {
       //正文
        CGFloat  retweetContentX = StatusCellBorderW;
        CGFloat  retweetContentY = StatusCellBorderW;

        
        Status * retweeted_status = status.retweeted_status;
        User * retweeted_user = retweeted_status.user;
        NSString * retweetContent  = [NSString stringWithFormat:@"@%@ : %@",retweeted_user.name,retweeted_status.text];
        

        CGSize retweetContentSize = [self sizeWithText:retweetContent font:StatusCellReweetContentFont maxW:maxW];
        self.retweetContentLabelF = (CGRect){{retweetContentX,retweetContentY},retweetContentSize};
        CGFloat retweetH = 0;//高度

        if (retweeted_status.pic_urls.count) {//有配图
            CGFloat retweetPhotoWH = 100;
            CGFloat retweetPhotoX = retweetContentX;
            CGFloat retweetPhotoY = CGRectGetMaxY(self.retweetContentLabelF) + StatusCellBorderW;
            //配图z
            self.retweetPhotoViewF = CGRectMake(retweetPhotoX, retweetPhotoY, retweetPhotoWH, retweetPhotoWH);
            retweetH = CGRectGetMaxY(self.retweetPhotoViewF) + StatusCellBorderW;//图片高度+10
        }else{
            
            retweetH = CGRectGetMaxY(self.retweetContentLabelF) + StatusCellBorderW;//label高度+10

        }
            //被转发微博整体
            CGFloat retweetX = 0;
            CGFloat retweetY = CGRectGetMaxY(self.originalViewF);
            CGFloat retweetW = cellW;
            

        self.retweetViewF = CGRectMake(retweetX, retweetY,retweetW , retweetH);
      
        self.cellHeight = CGRectGetMaxY(self.retweetViewF);
        toolBarY = CGRectGetMaxY(self.retweetViewF);//如果没有转发 从转发微博y开始
    }else{
        
        toolBarY = CGRectGetMaxY(self.originalViewF);//如果没有转发 从原创微博y开始
    }
    /**
     *  工具条
     */
    
    CGFloat toolbarX = 0;
    CGFloat toolbarW = cellW;
    CGFloat toolbarH = 35;
    self.toolbarF = CGRectMake(toolbarX, toolBarY, toolbarW, toolbarH);
    
    /* cell高度*/
    self.cellHeight = CGRectGetMaxY(self.toolbarF) ;



    
    
}
@end
